package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 查询用户资料
     *
     * @param userId
     * @param token
     * @return
     */


    @GetMapping
    public ResponseEntity users(Long userId, @RequestHeader("Authorization") String token)  {
        //判断token是否合法
        //获取用户ID

        if (userId == null) {
            userId = UserHolder.getUserId();
        }
        //UserInfo userInfo = userInfoService.findById(userId);
        UserInfoVo userInfo = userInfoService.findById(userId);
        return ResponseEntity.ok(userInfo);
    }


    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token) {
        //.设置用户id
        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);


    }
}
